package coolq.yaozhenyong.custom;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.forte.qqrobot.beans.messages.msgget.GroupMemberIncrease;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberReduce;
import com.forte.qqrobot.component.forlemoc.beans.msgget.MsgGroup;
import com.forte.qqrobot.utils.CQCodeUtil;

public class HtmlAndQq {
	
	public static String getMsghtml(String response, CQCodeUtil cqCodeUtil, MsgGroup msg) {
		response = htmltoCq(response);
		response = htmltoUser(response,cqCodeUtil,msg.getQQ());
		return response;
	}
	/***
	 * 退群
	 * @param inputs
	 * @param cqCodeUtil
	 * @param msg
	 * @return
	 */
	public static String groupMemberReduce(String inputs, CQCodeUtil cqCodeUtil, GroupMemberReduce msg) {
		//对时间变量的处理
		inputs = htmltoCq(inputs);
		inputs = htmltoUser(inputs,cqCodeUtil,msg.getBeOperatedQQ());
		return inputs;
	}
	/***
	 * 	进群欢迎语
	 * @param msg2 
	 * @param cqCodeUtil 
	 * @return
	 */
	public static String msggotograde(String msg, CQCodeUtil cqCodeUtil, GroupMemberIncrease msg2) {
		//对时间变量的处理
		msg = htmltoCq(msg);
		
		//获取用户的name
		msg = htmltoUser(msg,cqCodeUtil,msg2.getBeOperatedQQ());
		return msg;
	}
	
	/****
	 * 	获取用户信息，并且跟换的操作
	 * @param msg
	 * @param cqCodeUtil
	 * @param qq 
	 * @return
	 */
	private static String htmltoUser(String msg, CQCodeUtil cqCodeUtil, String qq) {
		if(qq!=null) {
			String[] face = getNumber(msg,"\\[@账号");
			if(face!=null) {
				for (int i = 0; i < face.length; i++) {
					msg = msg.replace("[@账号]", cqCodeUtil.getCQCode_at(qq));
				}
			}
			
		}
		if(qq!=null) {
			String[] face = getNumber(msg,"\\[账号");
			if(face!=null) {
				for (int i = 0; i < face.length; i++) {
					msg = msg.replace("[账号]", qq);
				}
			}
			
		}
		return msg;
	}


	/****
	 * 	对时间变量的处理
	 * @param msg
	 * @return
	 */
	private static String htmltoCq(String msg) {
		String[] at = getNumber(msg,"\\[当前时间");
		if(at!=null) {
			for (int i = 0; i < at.length; i++) {
				Date time = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateString = formatter.format(time);
				msg = msg.replace("[当前时间]", dateString);
			}
		}
		
		
		String[] data = getNumber(msg,"\\[获取时间");
		if(data!=null) {
			for (int i = 0; i < data.length; i++) {
				String dateString = "yyyy-MM-dd HH:mm:ss";
				try {
					Date time = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat(data[i]);
					dateString = formatter.format(time);
				}catch (Exception e) {
					// TODO: handle exception
				}
				msg = msg.replace("[获取时间"+data[i]+"]",dateString);
			}
		}
		
		return msg;
	}



	/****
	 * 转换工具
	 * html码转换成cq认可的码
	 * @param msg
	 * @param cqCodeUtil
	 * @return
	 *//*
	public static String getMsghtml(String msg,CQCodeUtil cqCodeUtil,String qq) {
		
		//转换face格式的图片,CQCodeUtil cqCodeUtil
		String[] face = getNumber(msg,"\\[face-");
		for (int i = 0; i < face.length; i++) {
			msg = msg.replace("[face-"+face[i]+"]", cqCodeUtil.getCQCode_face(face[i]));
		}
		
		String[] bface = getNumber(msg, "\\[bface-");
		for (int i = 0; i < bface.length; i++) {
			msg = msg.replace("[bface-"+bface[i]+"]", cqCodeUtil.getCQCode_bface(bface[i]));
		}
		String[] sface = getNumber(msg, "\\[sface-");
		for (int i = 0; i < bface.length; i++) {
			msg = msg.replace("[bface-"+sface[i]+"]", cqCodeUtil.getCQCode_sface(sface[i]));
		}
		String[] image = getNumber(msg, "\\[image-");
		for (int i = 0; i < bface.length; i++) {
			msg = msg.replace("[bface-"+image[i]+"]", cqCodeUtil.getCQCode_image(image[i]));
		}
		String[] record = getNumber(msg, "\\[image-");
		for (int i = 0; i < bface.length; i++) {
			msg = msg.replace("[bface-"+record[i]+"]", cqCodeUtil.getCQCode_record(record[i]));
		}
		String[] at = getNumber(msg, "\\[@-");
		for (int i = 0; i < bface.length; i++) {
			msg = msg.replace("[bface-"+at[i]+"]", cqCodeUtil.getCQCode_at(at[i]));
		}
		
		if(qq!=null) {
			String[] face = getNumber(msg,"\\[@当事人");
			for (int i = 0; i < face.length; i++) {
				msg = msg.replace("[@当事人]", cqCodeUtil.getCQCode_at(qq));
			}
		}
		
		
		String[] at = getNumber(msg,"\\[当前时间");
		for (int i = 0; i < at.length; i++) {
			Date time = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(time);
			msg = msg.replace("[当前时间]", dateString);
		}
		
		String[] data = getNumber(msg,"\\[获取时间");
		for (int i = 0; i < data.length; i++) {
			String dateString = "yyyy-MM-dd HH:mm:ss";
			try {
				Date time = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat(data[i]);
				dateString = formatter.format(time);
			}catch (Exception e) {
				// TODO: handle exception
			}
			msg = msg.replace("[获取时间"+data[i]+"]",dateString);
		}
		
		
		return msg;
	}*/
	

	
	
	public static String[] getNumber(String msg,String code) {
		String[] split = msg.split(code);
		if(split.length>1) {
			//将所有图片的文件名取出
			String a[] = new String[split.length-1];
			for (int i = 1; i < split.length; i++) {
				a[i-1] = split[i].substring(0, split[i].indexOf("]"));
			}
			return a;
		}
		return null;
	}
	

	
}
